package o;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import com.huawei.hwCloudJs.d.d;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.regex.Pattern;

public class doo {
    private static Proxy ˊ = null;
    private static int ˋ = -1;
    private static final Pattern ˏ = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
    private static int ॱ = -1;

    public static boolean ˊ(Context context) {
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService("connectivity");
            if (connectivityManager != null) {
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
                    return true;
                }
            }
        }
        return false;
    }

    public static String ˎ(Context context) {
        if (context != null) {
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getApplicationContext().getSystemService("connectivity")).getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
                return activeNetworkInfo.getTypeName();
            }
        }
        return "";
    }

    public static Proxy ˎ() {
        return ˊ;
    }

    public static void ˊ(Proxy proxy) {
        ˊ = proxy;
    }

    public static void ˎ(int i) {
        ˋ = i;
    }

    public static boolean ॱ(Context context) {
        ˏ(context);
        return ˋ == -3;
    }

    public static Proxy ˋ(Context context) {
        if (ॱ(context)) {
            return ˎ();
        }
        return null;
    }

    public static int ˏ(Context context) {
        if (context != null) {
            ˏॱ(context);
        }
        return ॱ;
    }

    public static int ʼ(Context context) {
        return ˊ(ˊॱ(context));
    }

    private static void ˏॱ(Context context) {
        ˎ(0);
        NetworkInfo ˊॱ = ˊॱ(context);
        if (ˊॱ == null) {
            Logger.ˋ("NetworkUtil", "setPsType() info = null");
            return;
        }
        ॱ = ˊ(ˊॱ);
        if (1 == ॱ) {
            return;
        }
        if (ˊ(ˊॱ, context)) {
            ˎ(-3);
        } else {
            ˎ(-2);
        }
    }

    private static NetworkInfo ˊॱ(Context context) {
        return ((ConnectivityManager) context.getApplicationContext().getSystemService("connectivity")).getActiveNetworkInfo();
    }

    public static boolean ॱॱ(Context context) {
        if (context != null) {
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.getType() == 1) {
                return activeNetworkInfo.isConnected();
            }
        }
        return false;
    }

    public static int ʽ(Context context) {
        try {
            Class cls = Class.forName("com.huawei.android.net.wifi.WifiManagerCommonEx");
            if (((Boolean) cls.getMethod("getHwMeteredHint", new Class[]{Context.class}).invoke(cls.newInstance(), new Object[]{context})).booleanValue()) {
                return 2;
            }
            return 1;
        } catch (ClassNotFoundException e) {
            Logger.ˎ("NetworkUtil", "isMeteredWifi ClassNotFoundException, ex: " + e.toString());
            return 0;
        } catch (NoSuchMethodException e2) {
            Logger.ˎ("NetworkUtil", "isMeteredWifi NoSuchMethodException, ex: " + e2.toString());
            return 0;
        } catch (IllegalAccessException e3) {
            Logger.ˎ("NetworkUtil", "isMeteredWifi IllegalAccessException: " + e3.toString());
            return 0;
        } catch (IllegalArgumentException e4) {
            Logger.ˎ("NetworkUtil", "isMeteredWifi IllegalArgumentException: " + e4.toString());
            return 0;
        } catch (InvocationTargetException e5) {
            Logger.ˎ("NetworkUtil", "isMeteredWifi InvocationTargetException: " + e5.toString());
            return 0;
        } catch (InstantiationException e6) {
            Logger.ˎ("NetworkUtil", "isMeteredWifi InstantiationException: " + e6.toString());
            return 0;
        }
    }

    public static boolean ᐝ(Context context) {
        return 2 == ʽ(context);
    }

    public static boolean ʻ(Context context) {
        if (context != null) {
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.getType() == 0) {
                return activeNetworkInfo.isConnected();
            }
        }
        return false;
    }

    private static boolean ˊ(NetworkInfo networkInfo, Context context) {
        int type = networkInfo.getType();
        boolean ˎ = dom.ˎ(context);
        if (type != 0 || !ˎ) {
            return false;
        }
        String host = android.net.Proxy.getHost(context);
        int port = android.net.Proxy.getPort(context);
        Proxy proxy = null;
        if (!(host == null || host.length() <= 0 || port == -1)) {
            proxy = new Proxy(Type.HTTP, new InetSocketAddress(host, port));
        }
        ˊ(proxy);
        return true;
    }

    public static int ˊ(NetworkInfo networkInfo) {
        if (networkInfo == null || !networkInfo.isConnected()) {
            return 0;
        }
        int type = networkInfo.getType();
        if (1 == type || 13 == type) {
            return 1;
        }
        if (type != 0) {
            return 0;
        }
        switch (networkInfo.getSubtype()) {
            case 1:
            case 2:
            case 4:
            case 7:
            case 11:
                return 2;
            case 3:
            case 5:
            case 6:
            case 8:
            case 9:
            case 10:
            case 12:
            case 14:
            case 15:
                return 3;
            case 13:
                return 4;
            case 20:
                return 5;
            default:
                return 0;
        }
    }

    public static boolean ˎ(boolean z) {
        if (!z) {
            return InetAddress.getByName("8.8.8.8").isReachable(2000);
        }
        try {
            return InetAddress.getByName("114.114.114.114").isReachable(2000);
        } catch (SecurityException e) {
            Logger.e("NetworkUtil", "SecurityException in isReachable method!");
            return true;
        } catch (UnknownHostException e2) {
            Logger.e("NetworkUtil", "UnknownHostException in isReachable method!");
            return true;
        } catch (IllegalArgumentException e3) {
            Logger.e("NetworkUtil", "IllegalArgumentException in isReachable method!");
            return true;
        } catch (IOException e4) {
            Logger.e("NetworkUtil", "IOException in isReachable method!");
            return true;
        }
    }

    public static boolean ॱ(String str, int i) {
        Socket socket;
        Throwable th;
        boolean z = false;
        if (!TextUtils.isEmpty(str)) {
            try {
                socket = new Socket();
                try {
                    socket.connect(new InetSocketAddress(str, i), 2000);
                    if (socket.isConnected()) {
                        z = true;
                    } else {
                        Logger.ˋ("NetworkUtil", "OTA host not connected!");
                    }
                    if (socket != null) {
                        try {
                            socket.close();
                        } catch (IOException e) {
                            if (Logger.ˎ()) {
                                Logger.ˏ("NetworkUtil", "Closeable IOException!");
                            }
                        }
                    }
                } catch (Exception e2) {
                    try {
                        Logger.ˋ("NetworkUtil", "OTA host Exception!");
                        if (socket != null) {
                            try {
                                socket.close();
                            } catch (IOException e3) {
                                if (Logger.ˎ()) {
                                    Logger.ˏ("NetworkUtil", "Closeable IOException!");
                                }
                            }
                        }
                        Logger.ˎ("NetworkUtil", "OTA host isReachable = " + z);
                        return z;
                    } catch (Throwable th2) {
                        th = th2;
                        if (socket != null) {
                            try {
                                socket.close();
                            } catch (IOException e4) {
                                if (Logger.ˎ()) {
                                    Logger.ˏ("NetworkUtil", "Closeable IOException!");
                                }
                            }
                        }
                        throw th;
                    }
                }
            } catch (Exception e5) {
                socket = null;
                Logger.ˋ("NetworkUtil", "OTA host Exception!");
                if (socket != null) {
                    socket.close();
                }
                Logger.ˎ("NetworkUtil", "OTA host isReachable = " + z);
                return z;
            } catch (Throwable th3) {
                th = th3;
                socket = null;
                if (socket != null) {
                    socket.close();
                }
                throw th;
            }
            Logger.ˎ("NetworkUtil", "OTA host isReachable = " + z);
        }
        return z;
    }

    public static int ˋॱ(Context context) {
        if (ॱॱ(context)) {
            try {
                WifiInfo connectionInfo = ((WifiManager) context.getSystemService(d.f)).getConnectionInfo();
                if (connectionInfo != null) {
                    return WifiManager.calculateSignalLevel(connectionInfo.getRssi(), 100);
                }
            } catch (Exception e) {
                Logger.e("NetworkUtil", "getWifiSignalLevel exception:" + e.getMessage());
                return -1;
            }
        }
        return -1;
    }

    public static boolean ॱ(String str) {
        return ˏ.matcher(str).matches();
    }

    public static boolean ͺ(Context context) {
        if (context != null) {
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
                int type = activeNetworkInfo.getType();
                Object obj = type == 0 ? 1 : null;
                Object obj2;
                if (type == 1) {
                    obj2 = 1;
                } else {
                    obj2 = null;
                }
                if (obj != null || (r0 != null && ᐝ(context))) {
                    return true;
                }
                return false;
            }
        }
        return false;
    }
}
